package com.layui.ad.controller;

import com.layui.ad.entity.Role;
import com.layui.ad.query.RoleQuery;
import com.layui.ad.service.RoleMenuService;
import com.layui.ad.service.RoleService;
import com.layui.ad.utils.JsonResult;
import com.layui.ad.vo.RolePermissionVo;
import com.layui.ad.vo.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;

    @GetMapping("/index")
    public String index(HttpServletRequest request){
        return "role/index";
    }

    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(RoleQuery query){
        return roleService.getList(query);
    }

    @GetMapping("/edit")
    public String edit(HttpServletRequest request,Integer id){
        Map<String,Object> info = new HashMap<>();
        if(null!=id && id>0){
            info = roleService.info(id);
        }
        request.setAttribute("info",info);
        return "role/edit.html";
    }

    @ResponseBody
    @PostMapping("/update")
    public JsonResult update(@RequestBody Role role){
        return roleService.edit(role);
    }

    @ResponseBody
    @PostMapping("/add")
    public JsonResult add(@RequestBody Role role){
        return roleService.edit(role);
    }


    @ResponseBody
    @GetMapping("/delete/{id}")
    public JsonResult delete(@PathVariable("id") Integer id){
        return roleService.delete(id);
    }

    @ResponseBody
    @GetMapping("/batchDelete/{ids}")
    public JsonResult batchDelete(@PathVariable("ids") String ids){
        return roleService.batchDelete(ids);
    }

    @ResponseBody
    @PostMapping("/setStatus")
    public JsonResult setStatus(@RequestBody Role role){
        return roleService.setStatus(role);
    }


    @ResponseBody
    @GetMapping("/getRolePermissionByRoleId")
    public JsonResult getRolePermissionByRoleId(Integer roleId){
        return roleMenuService.getRolePermissionByRoleId(roleId);
    }


    /**
     * 设置角色权限
     *
     * @param rolePermissionDto 角色菜单权限
     * @return
     */
    @ResponseBody
    @PostMapping("/setRolePermission")
    public JsonResult setRolePermission(@RequestBody RolePermissionVo rolePermissionVo) {
        return roleMenuService.setRolePermission(rolePermissionVo);
    }

}
